/**
 * Created by Administrator on 2017/3/27 0027.
 */
import React from "react";
import Confirm from '../../../common/Confirm'
import Select from '../../../common/Select'
import InputBox from '../../../common/InputBox'
import PackTradeApply from './PackTradeApply'
import Relay from "react-relay";
import GetParentOrderMutation from "../mutation/GetParentOrderMutation";
import GetEnterpriseListMutation from "../mutation/GetEnterpriseListMutation"
import FindUsersByEnterpriseIdAndPidMutation from "../mutation/FindUsersByEnterpriseIdAndPidMutation"
import DetailUploadModal from "./DetailUploadModal"
import ReactKey from "../../../util/ReactKey"
import Util from "../../../util/Util"
import DelQiniuFileMutation from "../mutation/DelQiniuFileMutation"
import './css/IndentManage.css'

export default class Createorder_StepOne extends React.Component{
    constructor(props){
        super(props);
        this.state={
            oders:[],
            partAordlist:[],
            enterpriseList:[],
            linkman:[],
            instructions:[]
        };
        this.instructions=[];
        this.odersobj={};
        this.fristchose = '';
        this.enterpriseList = [{text:"请选择客户",value:"0"}]
        this.enterprisedeobj={}
        this.linkman = [{text:'请选择联系人',value:'0'}];
    }

    componentWillMount(){
        this.ispartA = this.props.ispartA;
        if(this.ispartA){

            Relay.Store.commitUpdate(
                new GetEnterpriseListMutation({
                    entype : '1'
                }),
                {
                    onFailure: () => {

                    },
                    onSuccess: (response) => {
                        let errorCode = response.getEnterpriseList.data.errorCode;
                        if(errorCode == 0){
                            let enterpriseList = response.getEnterpriseList.data.enterpriseList;
                            enterpriseList.map((context,index)=>{
                                this.enterpriseList.push({text:context.enterpriseName,value:context.enterpriseId});
                                this.enterprisedeobj[context.enterpriseId] = {"address":context.address,"enterpriseName":context.enterpriseName}
                            });
                            this.setState({
                                enterpriseList : this.enterpriseList,
                                linkman:this.linkman
                            });
                        }
                    }
                }
            )

        }else{
            Relay.Store.commitUpdate(
                new GetParentOrderMutation({
                    enId : this.props.enterpriseId
                }),
                {
                    onFailure: () => {

                    },
                    onSuccess: (response) => {
                        let errorCode = response.getParentOrder.getParentOrder.errorCode;
                        if(errorCode==0){
                            let orders = response.getParentOrder.getParentOrder.orders;
                            let partAordlist = [];
                            orders.map((context,index)=>{
                                if(index==0){
                                    this.fristchose = context.oid
                                }
                                partAordlist.push({text:context.oid,value:context.oid});
                                this.odersobj[context.oid] = {'partAName':context.partAName,'account':context.account}
                            });
                            this.setState({
                                partAordlist:partAordlist
                            },()=>{
                                this.choseorder(this.fristchose);
                            });
                        }
                    }
                }
            )
        }
    }

    attach(){
        return  this.state.instructions.map((item,index)=>{
            return(
                <PackTradeApply  key={ReactKey.key} item={item} delqiniufile={this.delqiniufile.bind(this)}/>
            );

        })
    }


    choseorder(oid){
        let partAName
        let account
        if(oid!=''){
            partAName = this.odersobj[oid].partAName;
            let accounttext = this.odersobj[oid].account;
            account = Number(accounttext)/100
        }

        this.refs.partA.setState({
            text:partAName||''
        });
        this.refs.partacount.setState({
            text:account
        });
    }

    choseem(id){
        if(id ==0){
            this.linkman = [{text:'请选择联系人',value:'0'}];
            this.setState({
                linkman:this.linkman
            });
            this.refs.emaddres.setState({
                text:''
            });
            return;
        }
       let address = this.enterprisedeobj[id].address;
        this.refs.emaddres.setState({
            text:address||''
        });
        this.linkman = [{text:'请选择联系人',value:'0'}];
        Relay.Store.commitUpdate(
            new FindUsersByEnterpriseIdAndPidMutation({
                enterpriseId : id,
                /*销售订单管理权限的用户*/
                pid:'11'
            }),
            {
                onFailure: () => {

                },
                onSuccess: (response) => {

                    let errorCode = response.findUsersByEnterpriseIdAndPid.findUsersByEnterpriseIdAndPid.errorCode;
                    if(errorCode=="0"){
                        let userModelList = response.findUsersByEnterpriseIdAndPid.findUsersByEnterpriseIdAndPid.userModelList;

                        userModelList.map((context,index)=>{
                            this.linkman.push({text:context.name+" "+context.phone,value:context.uid})
                        });
                        this.setState({
                            linkman:this.linkman
                        });
                    }
                }
            }
        )
    }

    addinstructions(){
        if(this.instructions.length>=5){
            alert('不能超过5个');
            return
        }
        this.refs.deup.setState({
            visible:true
        });
    }

    addpact(name,hash,url,fileKey){
        this.instructions.push({"name":name,"hash":hash,"url":url,"fileKey":fileKey});
        this.setState({
            instructions:this.instructions
        });
    }

    delqiniufile(item,fileKye){
        Relay.Store.commitUpdate(
            new DelQiniuFileMutation({
                fileKey : fileKye
            }),
            {
                onFailure: () => {

                },
                onSuccess: (response) => {
                    let errorCode = response.delQiniuFile.delQiniuFile.errorCode;
                    if(errorCode==0){
                        Util.removedarrayitem(this.instructions,item);
                        this.setState({
                            instructions:this.instructions
                        });
                    }
                }
            }
        )
    }


    render(){
        if(this.ispartA){
            return(
                <div>
                    {/*<p className="create_order_step">第一步：添加客户信息*/}
                        {/*<span className="create_order_step_line"/>*/}
                    {/*</p>*/}

                    <InputBox style={{width:270,height:27,borderColor:"#36b3b3",paddingRight:"742px",marginTop:"20px"}} titletext={"客户："} ref={'client'}/>
                    <InputBox style={{width:270,height:27,borderColor:"#36b3b3",paddingRight:"742px",marginTop:"20px"}} titletext={"联系人："} ref={'linkman'}/>
                    <InputBox style={{width:270,height:27,borderColor:"#36b3b3",paddingRight:"742px",marginTop:"20px"}} titletext={"合同编号："} ref={'contractnum'}/>
                    <DetailUploadModal ref={'deup'} addpact={this.addpact.bind(this)} mid={'stpone'}/>
                </div>
            )
        }else{
            return(
                <div>
                    {/*<p className="create_order_step">第一步：添加上游订单*/}
                        {/*<span className="create_order_step_line"/>*/}
                    {/*</p>*/}

                    <p style={{width: 1094, height: 58, paddingLeft: "61px",position:"relative",marginTop:"10px"}}>
                        上游订单：
                        <Select  className="createorder_stepone_select" oArr={this.state.partAordlist}  style={{marginTop:"20px",marginLeft: '1px',width:293,height:27,display:"block",borderColor:"#36b3b3"}} onselectchose={this.choseorder.bind(this)} ref={'partAord'}/>
                        <span style={{position:"absolute",left:"32px",top:"21px",color:"red",display:"block",fontSize:"16px"}}>*</span>
                    </p>
                    <InputBox className="stepone_input1" style={{width:270,height:27,paddingRight:"780px",marginTop:"20px"}} titletext={"客户："} ref={"partA"} disabled={true}/>
                    <InputBox className="stepone_input1" style={{width:270,height:27,marginTop:"20px",paddingRight:"780px"}} titletext={"订单金额："} ref={"partacount"} disabled={true}/>
                </div>
            )
        }
    }
}
